package com.wan37.logic.tradingshop;

import com.wan37.logic.domain.vo.BackpackVO;
import com.wan37.logic.domain.vo.WeaponryVO;
import com.wan37.logic.email.EmailManager;
import com.wan37.logic.trading.util.TradingRouteUtil;
import com.wan37.logic.tradingshop.cache.TradingShopCache;
import com.wan37.logic.tradingshop.domain.vo.TradingShopRecordVO;
import com.wan37.logic.tradingshop.domain.vo.TradingShopVO;
import com.wan37.logic.tradingshop.util.TradingShopRouteUtil;
import com.wan37.protobuf.constant.CommonTradingShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-08-06 10:40
 **/
@Component
public class TradingShopManager {
    @Autowired
    private EmailManager emailManager;
    @Autowired
    private TradingShopRouteUtil tradingShopRouteUtil;
    @Autowired
    private TradingShopCache tradingShopCache;

    public void addBuyerWeaponry(String buyerId, String content, TradingShopVO tradingShopVO){
        //构建发送邮件武器附件
        List<WeaponryVO> newWeaponryList = new ArrayList<>();
        WeaponryVO newWeaponryVO = new WeaponryVO(tradingShopVO);
        newWeaponryList.add(newWeaponryVO);
        List<String> userIdList = new ArrayList<>();
        userIdList.add(buyerId);
        List<BackpackVO> backpackVOList = new ArrayList<>(newWeaponryList);
        emailManager.sendEmail(content, "拍卖行！", userIdList, backpackVOList);
    }
    public void getAll(String userId){
        List<TradingShopVO> tradingShopVOList = new LinkedList<>();
        List<TradingShopRecordVO> tradingShopRecordVOList = new LinkedList<>();
        for (TradingShopCache.TradingShopIn tradingShopIn :
                tradingShopCache.getTradingShopInMap().values()){
            tradingShopVOList.add(tradingShopIn.getTradingShopVO());
            if (tradingShopIn.getTradingShopRecordVOList().size() > 0){
                tradingShopRecordVOList.add(tradingShopIn.getTradingShopRecordVOList().get(0));
                for (int i = 1; i < tradingShopIn.getTradingShopRecordVOList().size(); i++){
                    if (tradingShopIn.getTradingShopRecordVOList().get(i).getBuyId().equals(userId)){
                        tradingShopRecordVOList.add(tradingShopIn.getTradingShopRecordVOList().get(i));
                    }
                }
            }
        }
        tradingShopRouteUtil.sendTradingShopResponse(CommonTradingShop.GET_ALL,
                userId, tradingShopVOList, tradingShopRecordVOList);
    }
}
